package objday03;

import java.util.Arrays;
import java.util.Scanner;

public class Atm {
    private User[] users = new User[1];
    private String bankName;
    private int card = 10000;
    private String welcome;
    private String userName;
    private String userPwd;
    private User loginUser;
    private String choose;
    private int moneys;
    private String cars;
    public Atm(){}
    Scanner scanner = new Scanner(System.in);
    public Atm(String bankName,String welcome){
        this.bankName = bankName;
        this.welcome = welcome;
        Admin admin = new Admin("admin","admin",true,true);
        users[0] = admin;
    }
    public void start(){
     while (true){
         welcomeView();
        chooseRun();
     }
    }
    public void chooseRun(){
        String choose = scanner.next();
        switch (choose){
           case "1":
            log:    while (true){
                    System.out.println("请输入你的账号:");
                    userName = scanner.next();
                    System.out.println("请输入你的密码:");
                    userPwd = scanner.next();
                if( login(userName,userPwd)){

                    break log;
                }
                continue log;

                }
                break;

            case "2":
             reg:   while (true) {
                    System.out.println("请输入你要注册的账号：");
                    userName = scanner.next();
                    System.out.println("请输入你的密码：");
                    userPwd = scanner.next();
                   if( register(userName,userPwd)){
                       break reg;
                   }
                   continue reg;

                }
                break;
            case "3":
                break;
            default:
                System.out.println("输入错误，请重新是呼入");
        }

    }
    private boolean login(String userName,String userPwd){
        if(userName == null||userName.isEmpty()||userPwd == null||userPwd.isEmpty()){
            System.out.println("账号或密码输入有误，请重新输入");
            return false;
        }
        for (User user : users) {
            if(user.getUserName().equals(userName)&&user.getUsePwd().equals(userPwd)){
                if(!user.isActive){
                    System.out.println("账户未激活，请联系管理员");
                    return false;
                }
                if("admin".equals(user.getUserName())){
                System.out.println("登录成功");
                AtmAction.run(users);
                loginUser = user;
                return true;
                }
                else {
                 a:   while (true){
                        System.out.println("欢迎用户用户登录");
                        System.out.println("请选择你要办理的业务");
                        System.out.println("1.查看余额。");
                        System.out.println("2.存钱");
                        System.out.println("3.转账");
                        System.out.println("4.查看个人信息");
                        System.out.println("5.退出");
                        choose = scanner.next();
                        Customer customer = (Customer)user;
                        switch (choose){
                            case "1":
                                System.out.println("你的余额为："+((Customer) user).money);
                                break;
                            case "2":
                                System.out.println("请输入你要存入的金额：");
                                moneys = scanner.nextInt();
                                ((Customer) user).money = String.valueOf(moneys+Integer.valueOf(((Customer) user).money));
                                System.out.println("存款成功");
                                break;
                            case "3":
                                System.out.println("请输入你要转入的账户：");
                                userName = scanner.next();
                                System.out.println("请输入你要转入的卡号:");
                                cars = scanner.next();
                                for (User user1 : users) {
                                    if (user1 instanceof Customer) {
                                        Customer customer1 = (Customer)user1;
                                        if(userName.equals(customer1.getUserName()) && cars.equals(customer1.card)){
                                            System.out.println("请输入你要转入的金额：");
                                            moneys = scanner.nextInt();
                                            if(Integer.valueOf(((Customer) user).getMoney())<Integer.valueOf(moneys)){
                                                System.out.println("你的余额不足,请重新输入");
                                                continue a;
                                            }
                                            customer1.money = String.valueOf(Integer.valueOf(customer1.getMoney())+Integer.valueOf(moneys));
                                            customer.money = String.valueOf(Integer.valueOf(customer.getMoney())-Integer.valueOf(moneys));
                                            System.out.println("转账成功。");
                                            continue a;
                                        }
                                    }
                                }
                                System.out.println("输入的信息有误，请重新输入。");
                                break ;

                            case "4":

                            case "5":
                                break a;
                            default:
                        }
                    }

                }

            }
        }
        System.out.println("登录失败,用户名或者账号有误");
        return false;
    }
    public boolean register(String userName,String userPwd){
        if(userName == null||userName.isEmpty()||userPwd == null||userPwd.isEmpty()){
            System.out.println("账号或密码输入有误，请重新输入");
            return false;
        }
        if("admin".equals(userName)){
            System.out.println("不能注册管理员账号");
            return false;  }

        for (User user : users) {
            if(user.getUserName().equals(userName)){
                System.out.println("该账号已被注册，请重新输入");
                return false;
            }
        }


        addLength();
        cardAdd();
        Customer customer = new Customer(userName,userPwd,false,false,String.valueOf(card),"20");
        users[users.length-1] = customer;
        System.out.println("注册成功");
        return true;
    }
    private void addLength(){
        users = Arrays.copyOf(users,users.length+1);
    }
    private void cardAdd(){
        card++;
    }

    public void welcomeView(){
        System.out.println(bankName+welcome);
        System.out.println("请选择你要办理的业务");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");

    }



}
